package com.howie.gulimall.search.controller;

import com.howie.gulimall.search.service.MallSearchService;
import com.howie.gulimall.search.vo.SearchParam;
import com.howie.gulimall.search.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: SearchController
 * @author: laizhenghua
 * @date: 2021/2/20 15:37
 */
@Controller
public class SearchController {
    @Autowired
    private MallSearchService searchService;
    /**
     * 跳转至list页面，自动将页面提交过来的所有请求查询参数封装成指定的对象
     * public static final String DEFAULT_PREFIX = "classpath:/templates/";
     * public static final String DEFAULT_SUFFIX = ".html";
     */
    @GetMapping("/list.html")
    public String toListPage(SearchParam param, Model model, HttpServletRequest request){
        String queryString = request.getQueryString();
        param.setQueryString(queryString);
        // 1、根据传递来的页面查询参数，去es中检索商品
        SearchResult data = searchService.search(param);
        model.addAttribute("result",data);
        return "list";
    }
}
